function getMessage() {
	var newCategory;
	var br="<br>";
	ui.box.load(U('blog/Index/addCategorys'), '添加分类',function (){
		    var select = $( '#select' );
		    select.children( ':first' ).attr( 'selected',true );
	});
}

function check(){
  if( $( '#title' ).val().length>30 ){
    alert( "标题不得大于30个中文字符" );
    return false;
  }
  if($('#select').val()=='0'){
	  alert('请选择分类');
	  return false;
  }
  return true;
}
$( document ).ready( function(){
    $( '#title' ).blur(function(){
      if( $( '#title' ).val().length>30  ){
        Alert( "标题不得大于30个中文字符" );
      }
      })

    });

    //判断是刷新还是关闭 
function   CloseOpen()   { 
    if(event.clientX <=0   &&   event.clientY <0){ 
    alert   ( "关闭 ") ;
    } 
} 
/**
 * addCategory
 * 添加分类
 * @param category $category
 * @access public
 * @return void
 */
function addCategory(tp){
    if( tp!="ok" ){
        var select = $( '#select' );
        select.children( ':first' ).attr( 'selected',true );
        return ui.box.close();
    }

    // 在发表日志时, 添加新分类
    var category = $('#newCategory').val();
    if($('#newCategory').val().length>10){
    	alert('分类名称不能大于10个中文字符');
        $("#newCategory").focus();
    	return false;
    }
    if(getLength(category.replace(/\s+/g,"")) == 0){
        alert('分类名不能为空');
        $("#newCategory").focus();
    	return false;
    }

	if( category != "" ){
    $.post( U('blog/Index/addCategory'),{name:category},function(txt){
        ui.box.close();

    	if( -1 == txt ){
        	ui.error( '添加失败' );
        }else if(  -2  == txt){
        	ui.error( '分类名冲突' );
        }else if( -3 == txt  ){
        	ui.error( '添加失败，分类名不能为空' );
        }else{
          $.post(U('blog/Index/filterCategory'), {name:category}, function(msg) {
        	  select = $( '#group' );
          	select.before( select.children(':first').clone().val(txt).html(msg));
          	$("#select option[value='"+txt+"']").attr( 'selected',true );
          	return;
          });
        }
  });
	}else{
		alert( '请输入分类名' );
        $("#newCategory").focus();
        return false;
	}

    var select = $( '#select' );
    select.children( ':first' ).attr( 'selected',true );
    return false;
}

/**
 * changePrivacy
 * 隐私按钮改变时
 * @param _this  $_this
 * @access public
 * @return void
 */
function changePrivacy( _this ){
  if( 3 == _this.val() ){
    $( '#password' ).show();
  }else{
    $( '#password' ).hide();
  }
}

function changeCategory( _this ){

	if( 0 == _this.val()){
		getMessage();
	}
}

/**
 * autosave
 * 自动保存
 * @param inst $inst
 * @param time  $time
 * @access public
 * @return void
 */
function autosave(){
  if( $( '#saveButton' ).attr( 'disabled' ) == "true" ){
    return;
  }
  $( '#saveButton' ).attr( 'disabled',true );
  //TODO 更换编辑器，这里必须修改；
  var content  = KE.util.getData('content');
  var title    = $( "input[name='title']" ).val( );
  var category = $( "select[name='category']" ).val();
  var privacy  = $( "select[name='privacy']" ).val();
  var mention  = $( "#ui_fri_ids" ).val();
  var password = $( "#password" ).val();
  var cc     = $( "#cc" ).val();
  //自动保存
  if( content.length>0 && $( '#saveId' ).val() == ""){
    //buttonclass = $( '#saveButton' ).attr( 'class' );
    //$( '#saveButton' ).attr( 'class','btn_w' );
    $.ajax( {
      type: 'POST',
      url: APP+'/Index/autosave',
      data:"title="+title+"&content="+content+"&category="+category+"&privacy="+privacy+"&password="+password+"&mention="+mention+"&cc="+cc,
      success:function( result ){
        if( result != -1 ){
          string = result.split( ',' );
          $( '#autoSave' ).html( title+" 已于"+string[0]+"保存在草稿箱" );
          $( '#saveId' ).val( string[1] );
          $( '#autoSave' ).fadeIn('slow');
              setTimeout( function(){
                $( '#autoSave' ).fadeOut( 'slow' );
                },3000 );
        }
        $( '#saveButton' ).removeAttr( 'disabled');
        //$( '#saveButton' ).attr( 'class',butonclass );
      }
      })
  }else{
	  $( '#saveButton' ).removeAttr( 'disabled');
    if($( '#saveId' ).val() == "") return false;
  }
  //修改自动保存的记录
  if ( $( '#saveId' ).val() != "" ){

    //buttonclass = $( '#saveButton' ).attr( 'class' );
    //$( '#saveButton' ).attr( 'class','btn_w' );
    var updata = $( '#saveId' ).val();
    $.ajax( {
      type: 'POST',
      url: APP+'/Index/autosave',
      data:"title="+title+"&content="+content+"&category="+category+"&privacy="+privacy+"&password="+password+"&mention="+mention+"&cc="+cc+"&updata="+updata,
      success:function( result ){
        if( result != -1 ){
          string = result.split( ',' );
          $( '#autoSave' ).html( title+" 已于"+string[0]+"保存在草稿箱" );
          $( '#saveId' ).val( string[1] );
          $( '#autoSave' ).fadeIn('slow');
              setTimeout( function(){
                $( '#autoSave' ).fadeOut( 'slow' );
                },3000 );
          $( '#saveButton' ).removeAttr( 'disabled');
        }
       // $( '#saveButton' ).removeAttr( 'disabled');
        //$( '#saveButton' ).attr( 'class',butonclass );
      }
      })

  }
  $( '#saveButton' ).removeAttr( 'disabled');
  return true;
}



/**
 * 异步提交表单
 * @param object form 表单DOM对象
 * @return void
 */
var ajaxSubmit = function(form) {
  var args = M.getModelArgs(form);
  M.getJS(THEME_URL + '/js/jquery.form.js', function() {
        var options = {
          dataType: "json",
            success: function(txt) {
            if(1 == txt.status) {
              if("function" ===  typeof form.callback) {
                form.callback(txt);
              } else {
                if("string" == typeof(args.callback)) {
                  eval(args.callback+'()');
                } else {
                  ui.success(txt.info);
                }
              }
            } else {
              ui.error(txt.info);
            }
            }
        };
        $(form).ajaxSubmit(options);
  });
};

/**
 * 处理ajax返回数据之后的刷新操作
 */
var ajaxReload = function(obj,callback){
    if("undefined" == typeof(callback)){
        callback = "location.href = location.href";
    }else{
        callback = 'eval('+callback+')';
    }
    if(obj.status == 1){
        ui.success(obj.data);
        setTimeout(callback,1500);
     }else{
        ui.error(obj.data);
    }
};
M.addEventFns({
  submit_btn: {
    click: function(){
      var title = $("#title").val();
      if( !title || getLength(title.replace(/\s+/g,"")) == 0){
       ui.error("标题不能为空");
       $("#title").focus();
       return false;
      }
      if(getLength($("#title").val()) > 25){
       ui.error("标题不得大于25个中文字符");
       $("#title").focus();
       return false;
      }
    //提交时编辑器需要先执行的方法
      E.sync();
      var content = $('#content').val();
      var contentNew = content.replace(/<\/?[^>]*>/g,'');//去除HTML tag
          contentNew = contentNew.replace(/&nbsp;/g,'')
      var contentLast = contentNew.replace(/[ | ]*\n/g,'\n'); //去除行尾空白
      if(contentLast == '' || getLength(contentLast) < 1){
        ui.error('日志内容不能为空');return false;
      }
      var args  = M.getEventArgs(this);
      if ( args.info && ! confirm( args.info )) {
        return false;
      }
      try{
        (function( node ) {
          var parent = node.parentNode;
          // 判断node 类型，防止意外循环
          if ( "FORM" === parent.nodeName ) {
            if ( "false" === args.ajax ) {
              ( ( "function" !== typeof parent.onsubmit ) || ( false !== parent.onsubmit() ) ) && parent.submit();
            } else {
              ajaxSubmit(parent);
            }
          } else if ( 1 === parent.nodeType ) {
            arguments.callee( parent );
          }
        })(this);
      }catch(e){
        return true;
      }
      return false;
    }
  }

});

M.addModelFns({
  event_post:{  //发布帖子
    callback:function(txt){
      ui.success('发布成功');
      setTimeout(function() {
        location.href = U('blog/Index/show')+'&id='+txt.data['id']+'&mid='+txt.data['mid'];
      }, 1500);
    }
  },
  event_edit:{  //编辑帖子
    callback:function(txt){
      ui.success('编辑成功');
      setTimeout(function() {
        location.href = U('blog/Index/show')+'&id='+txt.data['id']+'&mid='+txt.data['mid'];
      }, 1500);
    }
  }

});